package com.example.roomwordssample;

import android.app.Application;
import android.os.AsyncTask;

import androidx.lifecycle.LiveData;

import java.util.List;

public class WordRepository {
    private WordDao wordDao;
    private LiveData<List<Word>> allWords;

    WordRepository(Application application){
        WordRoomDatabase database=WordRoomDatabase.getDatabase(application);
        wordDao=database.wordDao();
        allWords=wordDao.getAllWords();
    }

    public LiveData<List<Word>>getAllWords(){
        return allWords;
    }

    public void insert(Word word){
 //       wordDao.insert(word);
        new InsertAsyncTask(wordDao).execute(word);
    }
    private static class InsertAsyncTask extends AsyncTask<Word,Void,Void>{
        private WordDao mAsyncTaskDao;

        public InsertAsyncTask(WordDao mAsyncTaskDao){
            this.mAsyncTaskDao=mAsyncTaskDao;
        }
        @Override
        protected Void doInBackground(Word... words){
            mAsyncTaskDao.insert(words[0]);
            return null;
        }
    }
}
